/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DataAccess;

import BussinesLogic.Request;
import DataModel.Persona;
import java.sql.*;

public class Dao_Persona {
        
    public Dao_Persona(){
    }
    
    public Request registrarPersona(Persona persona) {
        String sql = "INSERT INTO \"Persona\" (identificacion, nombre, apellido, lugar_nac, fecha_nac)"
                + " VALUES('" + persona.getIdentificacion() + "','" + persona.getNombre() + "','" + persona.getApellido()
                + "','" + persona.getLugarNac() + "','" + persona.getFechaNac() + "')";
        Request request = new Request();

        try {
            Connect conn = new Connect();
            driverPGSQL driver = new driverPGSQL(conn);
            request = driver.queryInsert(sql);
            conn.close();
        } catch (Exception e) {
            request.setTransac(false);
            request.setMessage(e.getMessage());
        }
        return request;
    }
    
    public Request eliminarPersona(Persona persona){
        
        String sql = "DELETE FROM \"Persona\" WHERE identificacion='" + persona.getIdentificacion() + "'";
        Request request = new Request();
        
        try{
            Connect conn = new Connect();
            driverPGSQL driver = new driverPGSQL(conn);
            request = driver.queryDelete(sql);
            conn.close();
        }
        catch(Exception e){
            request.setTransac(false);
            request.setMessage(e.getMessage());
        }
        
        return request;
    }
    
    
    public Request registrarNacionalidad(String identificacion, String nacionalidad) {
        String sql = "INSERT INTO \"Nacionalidad\" (identificacion, nacionalidad)"
                + " VALUES('" + identificacion + "','" + nacionalidad + "')";
        Request request = new Request();

        try {
            Connect conn = new Connect();
            driverPGSQL driver = new driverPGSQL(conn);
            request = driver.queryInsert(sql);
            conn.close();
        } catch (Exception e) {
            request.setTransac(false);
            request.setMessage(e.getMessage());
        }
        return request;

    }
    
    public Request eliminarNacionalidad(Persona persona) {

        String sql = "DELETE FROM \"Nacionalidad\" WHERE identificacion='" + persona.getIdentificacion() + "'";
        Request request = new Request();

        try {
            Connect conn = new Connect();
            driverPGSQL driver = new driverPGSQL(conn);
            request = driver.queryDelete(sql);
            conn.close();
        } catch (Exception e) {
            request.setTransac(false);
            request.setMessage(e.getMessage());
        }

        return request;
    }
 
    public Request consultarPersona(String campo1, String valor1, String campo2, String valor2){
        
        Request request = new Request();
        Persona p = new Persona();
        String sql = "SELECT * FROM \"Persona\" WHERE \"" + campo1 + "\"='" + valor1 + "' "
                + "AND " + campo2 + "='" + valor2 + "'";
        
        try {
            Connect conn = new Connect();
            driverPGSQL driver = new driverPGSQL(conn);
            request = driver.querySelect(sql);
            ResultSet rqs = request.getRequest();
            while (rqs.next()) {
                p.setIdentificacion(rqs.getString(1));
                p.setNombre(rqs.getString(2));
                p.setApellido(rqs.getString(3));
                p.setLugarNac(rqs.getString(4));
                p.setFechaNac(rqs.getDate(5));
            }
            request.setObject(p);
            conn.close();
        } catch (Exception e) {
            request.setTransac(false);
            request.setMessage(e.getMessage());
        }
        
        return request;
    }
}
